package edu.csl.study.springboot.admin.client.task;

import edu.csl.study.springboot.admin.client.controller.IndexControllor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
public class ScheduleConfig implements SchedulingConfigurer {
    Logger logger = LoggerFactory.getLogger(ScheduleConfig.class);
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(customScheduler());
    }

    @Bean(destroyMethod = "shutdown")
    public ExecutorService customScheduler() {
        return Executors.newScheduledThreadPool(20);
    }



    // 当方法的执行时间超过任务调度频率时，调度器会在下个周期执行
    @Scheduled(cron = "0/10 * * * * *")
    private void cronSchedule() {
        logger.info("---------cron表达式cronSchedule---------");
    }
    // 固定间隔，等上一次调度完成后，再开始计算间隔，再执行
    @Scheduled(fixedDelay = 5000)
    public void fixedDelaySchedule() {
        logger.info("---------固定间隔fixedDelaySchedule---------");
    }
    // 固定频率，如果上次调度超过了频率时间，那么在其完成后，立刻执行
    @Scheduled(fixedRate = 3000)
    public void fixedRateSchedule() {
        logger.info("---------固定频率 fixedRateSchedule-----");
    }

}
